package store.controller;

import store.dao.GoodsDAO;
import store.dao.RestockDAO;
import store.po.Goods;
import store.po.Restock;
import store.utils.Pager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/restock/list")
public class ListRestock extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String goods=request.getParameter("goodsName");
        String supplier=request.getParameter("supplierName");
        String startDate=request.getParameter("startDate");
        String endDate=request.getParameter("endDate");
        if(goods==null) goods="";
        if(supplier==null) supplier="";
        String now=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        if(startDate==null || "".equals(startDate)) startDate=now;
        if(endDate==null || "".equals(endDate)) endDate=now;
        Pager pager = (Pager) request.getAttribute("pager");
        try {
            List<Restock> restockList=new RestockDAO().findRestockes(goods,supplier,startDate,endDate,pager);
            request.setAttribute("restockList",restockList);
            request.getRequestDispatcher("/restock.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());

        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
